package com.googlecode.roleaccesscontrol.uc.user;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.PackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.google.common.collect.Lists;
import com.googlecode.roleaccesscontrol.entity.Application;
import com.googlecode.roleaccesscontrol.entity.Profile;
import com.googlecode.roleaccesscontrol.entity.User;
import com.googlecode.roleaccesscontrol.wicket.BasePage;
import com.googlecode.roleaccesscontrol.wicket.common.YesNoListChoice;

public class UserListPage extends BasePage {

	@SpringBean
	private UserRepository userRepository;

	private Application selectedApplication;
	private Profile selectedProfile;

	private UserDataProvider userDataProvider;

	public UserListPage() {
		add(new FeedbackPanel("feedback"));
		Form<User> form = new Form<User>("form", new CompoundPropertyModel(new User())) {
			@Override
			protected void onSubmit() {
				userDataProvider.setFilterParameters(new UserFilter(getModelObject(), selectedApplication,
						selectedProfile));
			}
		};
		add(form);

		form.add(new TextField("name"));
		form.add(new TextField("fullName"));
		form.add(new YesNoListChoice("active").setNullValid(true));

		final ListChoice<Profile> profiles = new ListChoice<Profile>("profiles",
				new PropertyModel<Profile>(this, "selectedProfile"),
				new LoadableDetachableModel<List<Profile>>() {
					@Override
					protected List<Profile> load() {
						if (selectedApplication == null)
							return new ArrayList();
						Profile prof = new Profile();
						prof.setName("simples");
						prof.setId(1L);
						return Arrays.asList(prof);
					}
				}, new IChoiceRenderer<Profile>() {
					public Object getDisplayValue(Profile profile) {
						return profile.getName();
					}

					public String getIdValue(Profile profile, int index) {
						return Long.toString(profile.getId());
					}
				}, 4);
		profiles.setOutputMarkupId(true);
		profiles.setNullValid(true);
		form.add(profiles);

		ListChoice<Application> applications = new ListChoice<Application>("applications",
				new PropertyModel<Application>(this, "selectedApplication"),
				new LoadableDetachableModel<List<Application>>() {
					@Override
					protected List<Application> load() {
						Application sist = new Application();
						sist.setId(1L);
						sist.setName("Sistema 1");
						return Arrays.asList(sist);
					}
				}, new IChoiceRenderer<Application>() {
					public Object getDisplayValue(Application app) {
						return app.getName();
					}

					public String getIdValue(Application app, int index) {
						return Long.toString(app.getId());
					}
				}, 1);
		applications.setNullValid(true);
		applications.add(new OnChangeAjaxBehavior() {
			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				target.addComponent(profiles);
			}
		});
		form.add(applications);

		List<IColumn<User>> userTableColumns = Lists.newArrayList();

		userTableColumns.add(new AbstractColumn<User>(new ResourceModel("username.column.header"),
				"name") {
			public void populateItem(Item<ICellPopulator<User>> cellItem, String componentId,
					IModel<User> rowModel) {

				Fragment fragment = new Fragment(componentId, "icon.and.username.fragment",
						UserListPage.this);
				cellItem.add(fragment);

				Link link = new Link("show.user.link") {
					@Override
					public void onClick() {

					}
				};
				fragment.add(link);

				User user = rowModel.getObject();
				boolean active = Boolean.TRUE.equals(user.getActive());
				link.add(new Image("icon.image", PackageResource.get(UserListPage.class,
						(active ? "" : "in") + "active-user-icon.png")));
				link.add(new Label("username.text", user.getName()));
			}
		});
		userTableColumns.add(new PropertyColumn<User>(new ResourceModel("fullname.column.header"),
				"fullName", "fullName"));

		this.userDataProvider = new UserDataProvider(userRepository);
		form.add(new DefaultDataTable<User>("user.list", userTableColumns, userDataProvider, 50));
		
		form.add(new Button("add.user.button") {
			@Override
			public void onSubmit() {
				setResponsePage(UserAddPage.class);
			}
		}.setDefaultFormProcessing(false));
	}

	@Override
	protected String getSuffixTitleString() {
		return getLocalizer().getString("page.title", this);
	}
}
